// const formatTime = date => {
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()
// //   return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
// return `${[year, month, day].map(formatNumber).join('-')} `
// }

const app = getApp()

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//从字符串中提取http链接(支持中文)
const get_url = string =>  {
    //reg的意思是http:// | https:// 任意字符 = ？ . /  & -      中文
    var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-|[\u4e00-\u9fa5])+)/g;
    if (reg.test(string)) {
        return string.match(reg)[0];
    } else {
        return "";
    }
}

//判断token是否存在
const hasToken = () => {
    let token = wx.getStorageSync('token')
    if (token) {
        return true;
    } else {
        return false;
    }
}

//时间格式化
const formatTime = item =>{
    const time = new Date() - new Date(item.createTime)
    const ipos = item.createTime.indexOf(' ')
    if (time / (1000 * 60 * 60 * 24 * 15 > 0)) {
        return item.createTime.substring(0,ipos)
    }else if (time / (1000 * 60 * 60 * 24 > 0)) {
        return parseInt(time / (1000 * 60 * 60 * 24)) + "天前"
    } else if (time / (1000 * 60 * 60 > 0)) {
        return parseInt(time / (1000 * 60 * 60)) + "小时前"
    } else {
        return parseInt(time / (1000 * 60)) + "分钟前"
    }
}

//分页
const pageHelper = (result,temp,that) =>{
 
    //分页加载
    if (result.length > 0) {
        //如果分页页码为1，说明用户在往下拉
        if (that.data.queryInfo.pageNumber == 1) {
            temp = [];
        }
        //如果结果长度大与分页长度，那么说明还可以是上拉加载新数据
        if (result.length >= that.data.queryInfo.pageSize) {
            that.setData({
                tableList: temp.concat(result),
                hasMoreDate: true,
                'queryInfo.pageNumber': that.data.queryInfo.pageNumber + 1
            })
        } else {
            that.setData({
                tableList: temp.concat(result),
                'queryInfo.pageNumber': that.data.queryInfo.pageNumber + 1,
                hasMoreDate: false
            })
            
        }
        //否则禁止上拉
    } else {
        console.log("没有更多数据了");
        that.setData({
            hasMoreDate: false
        })
        //在当前页显示加载动画
        wx.showNavigationBarLoading();
        //显示loading提示框
        wx.showLoading({
            title: '没有更多数据了'
        });
        setTimeout(() => {
            wx.hideNavigationBarLoading()
            wx.hideLoading();
        }, 1000);
    }
}

const toMarkdown = content =>{
        if (content) {
            content = app.towxml(content, 'markdown', {
                theme: 'light'
            })
            return content;
        }
}

const toHtml = content =>{
    if (content) {
        content = app.towxml(content, 'html', {
            theme: 'light'
        })
        return content;
    }
}


module.exports = {
  formatTime,
  get_url,
  hasToken,
  pageHelper,
  toMarkdown,
  toHtml
}

